package iterator;


import labelheap.*;
import global.*;
import bufmgr.*;
import diskmgr.*;


import java.lang.*;
import java.io.*;

/**
 *open a heapfile and according to the condition expression to get
 *output file, call get_next to get all Labels
 */
public class LabelFileScan extends  LabelIterator
{
	private LabelHeapFile labelHeap;
	private LScan scan;
	
	/**
	 *constructor
	 *@param file_name heapfile to be opened
	 *@param in1[]  array showing what the attributes of the input fields are. 
	 *@param s1_sizes[]  shows the length of the string fields.
	 *@param len_in1  number of attributes in the input Label
	 *@param n_out_flds  number of fields in the out Label
	 *@param proj_list  shows what input fields go where in the output Label
	 *@param outFilter  select expressions
	 *@exception IOException some I/O fault
	 *@exception FileScanException exception from this class
	 *@exception LabelUtilsException exception from this class
	 *@exception InvalidRelation invalid relation 
	 */
	public  LabelFileScan (String  file_name)
	throws IOException,
	FileScanException,
	LabelUtilsException
	{
		try {
			labelHeap = new LabelHeapFile(file_name);
		}
		catch(Exception e) {
			throw new FileScanException(e, "Create new labelheapfile failed");
		}

		try {
			scan = labelHeap.openLScan();
		}
		catch(Exception e){
			throw new FileScanException(e, "openScan() failed");
		}
	}

	/**
	 *@return the result label
	 *@exception JoinsException some join exception
	 *@exception IOException I/O errors
	 *@exception InvalidlabelSizeException invalid label size
	 *@exception InvalidTypeException label type not valid
	 *@exception PageNotReadException exception from lower layer
	 *@exception PredEvalException exception from PredEval class
	 *@exception UnknowAttrType attribute type unknown
	 *@exception FieldNumberOutOfBoundException array out of bounds
	 *@exception WrongPermat exception for wrong FldSpec argument
	 */
	public Label get_next()
			throws IOException,
			InvalidLabelSizeException,
			InvalidTypeException,
			PageNotReadException, 
			PredEvalException
			{     
		
		LID lid = new LID();;
		Label l;
		while(true) {
			l =  scan.getNext(lid);
			if(l == null) {
				return null;
			}     
			else
			{
				return l;
			}
			
		}
	}

	/**
	 *implement the abstract method close() from super class Iterator
	 *to finish cleaning up
	 */
	public void close() 
	{

		if (!closeFlag) {
			scan.closescan();
			closeFlag = true;
		} 
	}

}


